// CoverTab.cpp : implementation file
//

#include "stdafx.h"
#include "moretunes.h"
#include "CoverTab.h"
#include ".\covertab.h"
#include "shareware.h"
#include "misc.h"

// CCoverTab dialog

IMPLEMENT_DYNAMIC(CCoverTab, CResizablePage)
CCoverTab::CCoverTab(CPrefTab *prefs, CAllTabs *allTabs)
	: CResizablePage(CCoverTab::IDD)
	, _refresh(FALSE)
	, _currentAlbum(0)
	, _prefs(prefs)
	, _allTabs(allTabs)
{
}

CCoverTab::~CCoverTab()
{
}

void CCoverTab::DoDataExchange(CDataExchange* pDX)
{
	CResizablePage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_C_PIC, _coverPic);
	DDX_Control(pDX, IDC_EDIT1, _coverMessage);
}


BEGIN_MESSAGE_MAP(CCoverTab, CResizablePage)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedSave)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedReload)
	ON_WM_PAINT()
//	ON_WM_RBUTTONDBLCLK()
ON_WM_LBUTTONDBLCLK()
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedLoad)
END_MESSAGE_MAP()


#define MESSAGE_COVER_WRITE "Can't save the cover. Try to change the cover folder in the preferences"
#define MESSAGE_COVER_OK "Cover saved to "
#define MESSAGE_COVER_NOT_AV "Cover not available"
#define MESSAGE_COVER_NO_ALBUM "You must load an album first"

// CCoverTab message handlers

BOOL CCoverTab::setResult(Album* album)
{
	_currentAlbum = album;
	displayAlbum();
	return FALSE;
}

BOOL CCoverTab::displayAlbum(void)
{
	if (!_refresh)
		return FALSE;
	if (_currentAlbum == NULL) {
		_coverPic.loadPic(PIC_ALIEN_BIG, 2);
		Invalidate();
		UpdateWindow();
		return TRUE;
	}
		
	// Clear
	CString file("");
	_coverMessage.SetWindowText("");

	CFile fp;
	if (fp.Open(_currentAlbum->getImageLargeFile(), CFile::modeRead)) {
		if (fp.GetLength() > BAD_PIC_SIZE) {
			file = _currentAlbum->getImageLargeFile();
		}
		fp.Close();
	}
	if (file.IsEmpty() && fp.Open(_currentAlbum->getImageFile(), CFile::modeRead)) {
		file = _currentAlbum->getImageFile();
		fp.Close();
	}
	_coverPic.loadPic(file, 2);
	Invalidate();
	UpdateWindow();
	return TRUE;
}

void CCoverTab::OnBnClickedSave()
{
	if (_prefs->_registration != REGISTRATION_OK) {
		CShareware shareware;
		shareware.DoModal();
		return;
	}
	if (!_currentAlbum) {
		_coverMessage.SetWindowText(MESSAGE_COVER_NO_ALBUM);
		return;
	}

	CString newFile = _prefs->_editCoverData+"\\"+windowsFilename(_currentAlbum->getArtist()+" - "+_currentAlbum->getTitle()+".jpg");
	char buffer [MAX_PATH];
	memset (buffer, 0, sizeof(buffer));
	VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), buffer, _MAX_PATH));
	CString sPath = (CString)buffer;
	sPath = sPath.Left(sPath.ReverseFind('\\'));
	sPath += "\\";

	CString srcFile = sPath+_currentAlbum->getImageLargeFile();
	CFile fp;
	if (fp.Open(srcFile, CFile::modeRead)) {
		if (fp.GetLength() < BAD_PIC_SIZE)
			srcFile = _currentAlbum->getImageFile();
		fp.Close();
	}
	if (!CopyFile(srcFile, newFile, FALSE)) {
		_coverMessage.SetWindowText(MESSAGE_COVER_WRITE);
		return;
	}	
	_coverMessage.SetWindowText(MESSAGE_COVER_OK+_prefs->_editCoverData);
}

void CCoverTab::OnBnClickedReload() {
	if (!_currentAlbum)
		return;
	CFileFind file;
	if (file.FindFile(_currentAlbum->getImageLargeFile())) {
		file.Close();
		CFile::Remove(_currentAlbum->getImageLargeFile());
	}
	_allTabs->downloadBigPic();
}

void CCoverTab::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	if (!_refresh) {
		_refresh = TRUE;
		displayAlbum();
	}
}
void CCoverTab::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	_allTabs->OnShowMini();
	CResizablePage::OnLButtonDblClk(nFlags, point);
}

#define OPENCOVER_DIALOG "Open a picture"
void CCoverTab::OnBnClickedLoad()
{
	if (_prefs->_registration != REGISTRATION_OK) {
		CShareware shareware;
		shareware.DoModal();
		return;
	}
	if (_currentAlbum == NULL) {
		_coverMessage.SetWindowText(MESSAGE_COVER_NO_ALBUM);
		return;
	}
	LPCSTR szFilters = "Image files |*.gif;*.jpg;*.png;*.jpeg;*.bmp|All Files(*.*)|*.*||";
	CFileDialog fileDlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, szFilters);

	// Display the file dialog. When user clicks OK, fileDlg.DoModal() 
	// returns IDOK.
	if (fileDlg.DoModal() == IDOK)
	{
		// Get the info
		CString pathName = fileDlg.GetPathName();
		CString fileName = fileDlg.GetFileTitle();
		CString	temp;

		// Set the working dir
		char buffer [MAX_PATH];
		memset (buffer, 0, sizeof(buffer));
		VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), buffer, _MAX_PATH));
		CString sPath = (CString)buffer;
		sPath = sPath.Left(sPath.ReverseFind('\\'));
		sPath += "\\";
		SetCurrentDirectory(sPath);

		// Copy the file
		temp = sPath;
		temp += CACHE_LOCATION;
		temp += _currentAlbum->getId().GetAt(0);
		temp += "\\";
		temp += _currentAlbum->getAsin();
		temp += FILE_SUFFIX;
		CopyFile(pathName, temp, FALSE);

		temp = sPath;
		temp += CACHE_LOCATION;
		temp += _currentAlbum->getId().GetAt(0);
		temp += "\\";
		temp += _currentAlbum->getAsin();
		temp += "_B";
		temp += FILE_SUFFIX;
		CopyFile(pathName, temp, FALSE);

		// Display the result
		displayAlbum();
		_allTabs->_albumsTab->_pic.loadPic(_currentAlbum->getImageFile());
	}
}
